package com.xtourms.client.sys.um;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.ContentFrame;
import com.xtourms.client.Service;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.client.util.Util;
import com.xtourms.shared.field.WorkerField;

public class WorkerDetailManager extends Composite {

	private static WorkerDetailManagerUiBinder uiBinder = GWT
			.create(WorkerDetailManagerUiBinder.class);

	interface WorkerDetailManagerUiBinder extends
			UiBinder<Widget, WorkerDetailManager> {
	}

	private JSONObject workerObj;
	private ContentFrame parent;
	private AsyncCallback<String> callback;
	
	public WorkerDetailManager(ContentFrame parent, AsyncCallback<String> asyncCallback) {
		initWidget(uiBinder.createAndBindUi(this));
		this.workerObj = new JSONObject();
		this.parent = parent;
		this.callback = asyncCallback;
		initWidget();
	}

	public WorkerDetailManager(JSONObject workerObj, ContentFrame parent,
			AsyncCallback<String> asyncCallback) {
		this.workerObj = workerObj;
		this.parent = parent;
		this.callback = asyncCallback;
		initWidget();
		initDetail();
	}

	@UiField TextBox worker_name;
	@UiField TextBox worker_username;
	@UiField PasswordTextBox worker_pwd;
	@UiField PasswordTextBox worker_pwd2;
	@UiField ListBox worker_dep;
	@UiField ListBox worker_droit;
	
	private void initWidget(){
		Util.fillListBox(worker_dep, WorkerField.DEP_STRS);
		Util.fillListBox(worker_droit, WorkerField.DROIT_STRS);
	}
	
	private void initDetail(){
		worker_name.setValue(((JSONString)workerObj.get(WorkerField.WORKER_NAME)).stringValue());
		worker_pwd.setValue(((JSONString)workerObj.get(WorkerField.WORKER_PWD)).stringValue());
		worker_pwd2.setValue(((JSONString)workerObj.get(WorkerField.WORKER_PWD)).stringValue());
		worker_username.setValue(((JSONString)workerObj.get(WorkerField.WORKER_USERNAME)).stringValue());
		int index = (int) (((JSONNumber)workerObj.get(WorkerField.WORKER_DEP)).doubleValue() - 1);
		worker_dep.setSelectedIndex(index);
		index = (int) (((JSONNumber)workerObj.get(WorkerField.WORKER_DROIT)).doubleValue() - 1);
		worker_droit.setSelectedIndex(index);
		worker_username.setEnabled(false);
	}
	
	@UiHandler("confirm")
	void confirm(ClickEvent evt){
		String userName = worker_username.getValue();
		if(userName.isEmpty()){
			MessageBox.ins.showWarning("请输入用户名!");
			return;
		}
		String pwd1 = worker_pwd.getValue();
		String pwd2 = worker_pwd2.getValue();
		if(pwd1.isEmpty() || pwd1.length() < 6){
			MessageBox.ins.showWarning("密码长度不够6位");
			worker_pwd.setValue("");
			worker_pwd2.setValue("");
			return;
		}
		if(!pwd1.equals(pwd2)){
			MessageBox.ins.showWarning("密码不一致!");
			worker_pwd.setValue("");
			worker_pwd2.setValue("");
			return;
		}
		
		workerObj.put(WorkerField.WORKER_DEP, new JSONNumber(worker_dep.getSelectedIndex()+1));
		workerObj.put(WorkerField.WORKER_DROIT, new JSONNumber(worker_droit.getSelectedIndex()+1));
		workerObj.put(WorkerField.WORKER_NAME, new JSONString(worker_name.getValue()));
		workerObj.put(WorkerField.WORKER_PWD, new JSONString(pwd1));
		workerObj.put(WorkerField.WORKER_USERNAME, new JSONString(userName));
		
		Service.umService.cOrMWorker(workerObj.toString(), callback);
	}
	
	@UiHandler("cancel")
	void back(ClickEvent evt){
		parent.resumeContent();
	}
}
